package com.jsgt.greekfire.ui.mainassets

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.fhx.core.base.BaseFragment
import com.fhx.core.goToActivity
import com.fhx.core.utils.PriceFormatUtil
import com.fhx.core.utils.ScreenUtil
import com.fhx.core.utils.StatusBarUtil
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.IS_ACTIVATION
import com.jsgt.greekfire.LOGIN_STATUS
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.AssetsBean
import com.jsgt.greekfire.bean.MainAssetsBean
import com.jsgt.greekfire.common.util.NetDataManager
import com.jsgt.greekfire.common.util.UtilsBigDecimal
import com.jsgt.greekfire.common.util.XLinearLayoutManager
import com.jsgt.greekfire.event.AssetsEvent
import com.jsgt.greekfire.ui.home.entrance.EntranceActivity
import com.jsgt.greekfire.ui.mainassets.adapter.MainAssetsAdapter
import com.jsgt.greekfire.ui.mainassets.coindetail.CurrencyDetailsActivity
import com.jsgt.greekfire.ui.mine.coindetail.recharge.RechargeActivity
import com.scwang.smartrefresh.header.MaterialHeader
import kotlinx.android.synthetic.main.fragment_main_assets.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.math.BigDecimal

class MainAssetsFragment : BaseFragment(), View.OnClickListener {
    private var assetlist: ArrayList<MainAssetsBean>? = ArrayList()

    private var totalAssets: BigDecimal? = BigDecimal.ZERO

    private val mainAssetsAdapter by lazy {
        MainAssetsAdapter(context!!, R.layout.item_main_assets_list)
    }
    private lateinit var assetsBean: AssetsBean


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ScreenUtil.setStatusBarColor(context!!, "#2F313F")
        StatusBarUtil.setStatusTextColor(false, activity!!)
    }

    override fun getLayoutId(): Int = R.layout.fragment_main_assets

    override fun main() {
        NetDataManager.getUserAssets()

        llRecharge.setOnClickListener(this)
        llTransfers.setOnClickListener(this)
        llConvert.setOnClickListener(this)
        recycler.layoutManager = XLinearLayoutManager(context!!)
        recycler.adapter = mainAssetsAdapter
        refreshAsset.setRefreshHeader(MaterialHeader(context!!))
        refreshAsset.setOnRefreshListener {
            NetDataManager.getUserAssets()
        }
        refreshAsset.setOnLoadMoreListener {
            NetDataManager.getUserAssets()
        }
        refreshAsset.autoRefresh()
        cbEye.setOnCheckedChangeListener { buttonView, isChecked ->
            mainAssetsAdapter.setShowMoney(isChecked);
            if (!isChecked) {
                txvTotal.setText("****")
            } else {
                if (txvTotal != null) {
                    txvTotal.setText("0.00")
                } else {
                    txvTotal.setText("0.00")
                }
            }
        }
        mainAssetsAdapter.setOnItemClickListener { adapter, view, position ->
            CurrencyDetailsActivity.start(context!!, mainAssetsAdapter.data.get(position))
        }

    }

    override fun onResume() {
        super.onResume()
        NetDataManager.getUserAssets()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAssetsEvent(e: AssetsEvent?) {
        if (assetlist != null) {
            assetlist?.clear()
        } else {
            assetlist = java.util.ArrayList()
        }
        totalAssets = BigDecimal.ZERO
        for (index in NetDataManager.assetsList.indices) {
            var mainAssetsBean = MainAssetsBean()
            mainAssetsBean.assetId = NetDataManager.assetsList.get(index).display
            mainAssetsBean.assetGuid = NetDataManager.assetsList.get(index).assetGuid
            if (NetDataManager.assetsList.get(index).assetName.equals("USDT")) {
                mainAssetsBean.image = R.mipmap.main_asset_usdt_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("MET")) {
                mainAssetsBean.image = R.mipmap.main_asset_met_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("ZWT")) {
                mainAssetsBean.image = R.mipmap.main_asset_zwb_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("LTT")) {
                mainAssetsBean.image = R.mipmap.main_asset_ltb_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("JJT")) {
                mainAssetsBean.image = R.mipmap.main_asset_jjb_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("FHT")) {
                mainAssetsBean.image = R.mipmap.main_asset_fhb_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("JFT")) {
                mainAssetsBean.image = R.mipmap.main_asset_jft_logo
            }
            mainAssetsBean.name = NetDataManager.assetsList.get(index).assetName
            mainAssetsBean.amount = BigDecimal(NetDataManager.assetsList.get(index).assetBalance.toString()).setScale(8,BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString()

            var num = UtilsBigDecimal.mul(
                NetDataManager.assetsList.get(index).assetBalance.toString(),
                NetDataManager.assetsList.get(index).price.toString(),
                8
            )
            mainAssetsBean.rmbrice = "≈$ " + num!!.stripTrailingZeros().toPlainString()
            assetlist!!.add(mainAssetsBean)
            totalAssets = UtilsBigDecimal.add(totalAssets.toString(), num.toString())
        }
        txvTotal.setText(totalAssets!!.stripTrailingZeros().toPlainString())
        mainAssetsAdapter.replaceData(assetlist!!)
        if (refreshAsset != null) {
            refreshAsset.finishRefresh();
            refreshAsset.finishLoadMore();
            refreshAsset.finishLoadMoreWithNoMoreData();
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.llRecharge -> {
                if (LOGIN_STATUS) {
                    startActivity(Intent(context!!, RechargeActivity::class.java))
                } else {
                    EntranceActivity.start(context!!)
                }
            }
            R.id.llTransfers -> {
                //showToast("转账")
                if (LOGIN_STATUS) {
                    if (IS_ACTIVATION == 1) {
                        ToastUtils.show(context!!, getString(R.string.account_no_activation))
                    } else {
                        goToActivity<NewTransferActivity>()
                    }
                } else {
                    EntranceActivity.start(context!!)
                }
            }
            R.id.llConvert -> {
                //showToast("兑换")
                if (LOGIN_STATUS) {
                    if (IS_ACTIVATION == 1) {
                        ToastUtils.show(context!!, getString(R.string.account_no_activation))
                    } else {
                        goToActivity<ConvertActivity>()

                    }
                } else {
                    EntranceActivity.start(context!!)
                }
            }

        }
    }
}